<template>
  <a-modal :open="open" width="960px" title="候诊区队列" centered @cancel="onCancel">
    <div class="h-[650px] overflow-y-scroll">
      <BaseTable :loading="loading" column-code="000715" :data-source="tableData" bordered :pagination="false">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <!-- 0 无效 1有效 -->
            <a-switch v-model:checked="record.status" :checked-value="1" :un-checked-value="0" @change="!record.isNew && saveRow(record)" />
          </template>
        </template>
      </BaseTable>
    </div>
    <template #footer>
      <a-button type="primary" ghost @click="onInit()">初始化</a-button>
      <a-button type="primary" @click="onCancel">关闭</a-button>
    </template>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'update:value', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id) {
        getData()
      }
    }
  }
)
const tableData = ref([])
const loading = ref(false)
const getData = async () => {
  loading.value = true
  const res = await post('/outp/dictQueue/getDictQueueList', { regionId: props.rowData.regionId })
  loading.value = false
  if (res.code !== 0) return message.error(res.msg || '获取数据失败')
  tableData.value = res.data || []
}
const onInit = async () => {
  const res = await post('/outp/dictQueue/initDictQueueList', { regionId: props.rowData.regionId })
  if (res.code !== 0) return message.error(res.msg || '初始化失败')
  getData()
}
const saveRow = async (row) => {
  const res = await post('/outp/dictQueue/doUpdateDictQueue', { ...row })
  if (res.code !== 0) return message.error(res.msg || '保存失败')
  message.success(res.msg || '保存成功')
}
const onCancel = () => {
  emit('update:open', false)
}
</script>
<style lang="less" scoped></style>
